//
//  RZConversation.m
//  RZIM
//
//  Created by reyzhang on 2021/8/2.
//

#import "RZConversation.h"
#import <MJExtension/MJExtension.h>
@implementation RZConversation
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{
        @"groupInfo":@"group",
    };
}

+ (NSDictionary *)mj_objectClassInArray {
    return @{
        @"userList":@"RZUserInfo",
        @"messageList" : @"RZMessageInfo",
        @"groupInfo" : @"RZGroupInfo",
    };
}


+ (instancetype)createWithSocketMessage:(RZMessage *)message {
    RZConversation *conversation = [RZConversation new];
    
    RZGroupInfo *groupInfo = [[RZGroupInfo alloc] init];
    groupInfo.gid = message.gid;
    groupInfo.notReadCnt = 1;
    //其他信息，在外面通过delegate获取
    conversation.groupInfo = groupInfo;
    
    RZMessageInfo *msgInfo = [RZMessageInfo createWithSocketMessage:message];
    if (msgInfo) {
        conversation.messageList = @[msgInfo].mutableCopy;
    }
    
    return conversation;
}



- (NSString *)targetId {
    if (self.groupInfo.groupType == 0) { //单聊，返回用户id
        return self.userList.firstObject.uid;
    }
    return self.groupInfo.gid; //返回组id
}



- (NSString *)targetName {
    if (self.groupInfo.groupType == 0) { //单聊， 返回用户名称
        return self.userList.firstObject.username;
    }
    return self.groupInfo.groupName;
}


- (NSString *)targetPortrait {
    if (self.groupInfo.groupType == 0) { //单聊，返回用户头像
        return self.userList.firstObject.userAvatar;
    }
    return self.groupInfo.groupAvatar;
}


- (RZConversationType)conversationType {
    return (RZConversationType)self.groupInfo.groupType;
}

@end
